package com.cmccgz.udacs_landlord_webapp.update;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.widget.Toast;


public class NetUtil {

	private final static Uri apn_uri = Uri.parse("content://telephony/carriers");
	private final static Uri preferred_apn_uri = Uri.parse("content://telephony/carriers/preferapn");
	private Context ctx;
	private ConnectivityManager cm;
	public NetUtil(Context ctx) {
		this.ctx = ctx;
		cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
	}
	
	/** 
	 * 对网络连接状态进行判断 
	 * @return  true, 可用； false， 不可用 
	 */  
	public static boolean isNetEnabled(Context context) {  
	    ConnectivityManager connManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);  
	    if(connManager.getActiveNetworkInfo() != null) {  
	        return connManager.getActiveNetworkInfo().isConnected();  
	    }
	    return false;  
	}
	
	public boolean setNetIsCmnet() {
		boolean result = false;
		try {
			NetworkInfo mobileNetWork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
			if(mobileNetWork.isConnectedOrConnecting()) {
				if(!"cmnet".equalsIgnoreCase(mobileNetWork.getExtraInfo())) {
					Cursor cursor = ctx.getContentResolver().query(apn_uri, null, " apn like '%cmnet%'", null, null);
					String apn_id = "";
					//获取需要切换的cmnet的id
					for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
						apn_id = cursor.getString(cursor.getColumnIndex("_id")).toLowerCase();
					}
					if(StringUtils.isEmpty(apn_id)) {
						return false;
					}
					cursor.close();
					//获取当前连接ID
					String now_apnid = "";
					Cursor current = ctx.getContentResolver().query(preferred_apn_uri, null, null, null, null);
					for(current.moveToFirst(); !current.isAfterLast(); current.moveToNext()) {
						now_apnid = current.getString(current.getColumnIndex("_id"));
					}
					ContentValues changeValues = new ContentValues();
					changeValues.put("apn_id", apn_id);
					ctx.getContentResolver().update(preferred_apn_uri, changeValues, null, null);
				}
			}
		} catch (Exception e) {
			result = false;
		}
		result = true;
		return result;
	}
	
	/**
	 * 检查网络是否未开启,并且跳转到网络设置界面
	 * @param context
	 */
	public boolean checkNet() {
		NetworkInfo networkInfo = cm.getActiveNetworkInfo();
		if(null == networkInfo || !networkInfo.isConnected()) {
			return false;
		}
		
		return true;
	}
	
	public void startNetWorkIntent() {
		AlertDialog.Builder dialog = new AlertDialog.Builder(ctx)
        .setTitle("提示")
        .setMessage("网络未连接,是否开启网络?")
        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
			}
		  })
		 .setPositiveButton("设置", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Intent intent = new Intent("android.settings.WIRELESS_SETTINGS");
				ctx.startActivity(intent);
			}
		});
		dialog.create();
		dialog.show();
	}
	
	public void toastNetUnconn() {
		Toast.makeText(ctx, "网络连接失败，请检查网络", Toast.LENGTH_LONG).show();
	}
}
